<?php
/**
 * 网站后台控制器
 * Created by ykinfo.net
 * User: zhangbf <zhangbf@ykinfo.net>
 * Date: 2017/6/27  17:41
 */
namespace app\admin\controller;

use app\common\model\Company;
use app\common\model\News;
use app\common\model\NewsType;
use controller\BasicAdmin;
use service\DataService;
use service\LogApiService;
use service\LogService;
use think\Db;


/**
 * 系统登录控制器
 * class Login
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/10 13:59
 */
class Sites extends BasicAdmin {

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemLog';

    /**
     * 当前页面标题
     * @var string
     */
    public $title = '新闻管理';

    /**
     * 日志列表
     */
    public function index() {
        $this->title = '系统操作日志';
        $this->assign('actions', Db::name($this->table)->group('action')->column('action'));
        $db = Db::name($this->table)->order('id desc');
        $get = $this->request->get();
        foreach (['action', 'content', 'username'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }
        return parent::_list($db);
    }

    /**
     * 公司模块
     * @return mixed
     */
    public function company()
    {
        $this->assign('title', '公司信息');

        if (!$this->request->isPost()) {
            //获取数据
            $row=Db::name('HnCompany')->where('is_deleted',0)->find();
            $data['data']=$row;
            $this->assign('data', $row);
            return view();
        }else{
            $data=$this->request->post();
            $companyModel=new Company();
            $res=$companyModel->saveCompany($data);
            if($res){
                LogService::write('网站管理', '网站信息保存或修改');
                $this->success('数据保存成功！', '');
            }else{
                $this->error('数据保存失败！', '');
            }
        }
    }

    /**
     * 新闻管理
     */
    public function news()
    {
        $this->title = '新闻列表';
        $newsType=new NewsType();
        $this->assign('types',$newsType->getType());

        $this->table="hn_news";//设置表
        $db = Db::name($this->table)->order('id desc');
        $db->where('is_deleted',0);
        $get = $this->request->get();
        foreach (['title', 'type', 'author'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }
        return parent::_list($db);
    }

    /**
     * 新增新闻
     * @return array|string
     */
    public function form_news()
    {
        if(!$this->request->isPost()){
            $newsType=new NewsType();
            $this->assign('types',$newsType->getType());

            $this->table="HnNews";
            return $this->_form($this->table, 'news_form');
        }else{
            //保存新闻数据
            $data=$this->request->post();
            $model=new News();
            $res=$model->saveNews($data);
            if($res){
                LogService::write('网站管理', '保存或更新新闻数据成功');
                $this->success('数据保存成功！', '');
            }else{
                $this->error('数据保存失败！', '');
            }
        }

    }


    /**
     * 删除新闻
     */
    public function del_news()
    {
//        LogApiService::write($this->request->post('id',""));
        $this->table="hn_news";
        $id = $this->request->post('id');
        if ($id) {
            $news=new News();
            $news->delNews($id);
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");


    }

    /**
     * 新闻类型
     */
    public function news_type()
    {
        $this->title = '新闻类型';
        $this->table="HnNewsType";
        $db = Db::name($this->table)->order('id desc');
        $db->where('is_deleted', 0);
        $get = $this->request->get();
        foreach (['name'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }

        return parent::_list($db);
    }

    /**
     * 类型添加
     */
    public function add_type() {
        $this->table="HnNewsType";
        return $this->_form($this->table, 'news_type_form');
    }

    /**
     * 类型编辑
     */
    public function edit_type() {
        $this->table="HnNewsType";
        return $this->_form($this->table, 'news_type_form');
    }

    /**
     * 类型删除
     */
    public function del_type() {
        $this->table="HnNewsType";
        $id = $this->request->post('id');
        if ($id) {
            $type=new NewsType();
            $type->deleteById($id);
            $this->success("类型删除成功！", '');
        }
        $this->error("类型删除失败，请稍候再试！");
    }
}